package servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.User;
import DAO.UserDao;

public class MemberModifyAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		HttpSession session=request.getSession();
		ActionForward forward=null;
		
		int userId = session.getAttribute("userid") != null
				&& session.getAttribute("userid") != "" ? Integer
				.parseInt((String)session.getAttribute("userid")) : 0;
				
				
		UserDao userDao = new UserDao();
		User user = new User();
		
		
		String passwd = request.getParameter("passwd") != null
				&& request.getParameter("passwd") != "" ? request
				.getParameter("passwd") : null;
		String name = request.getParameter("name") != null
				&& request.getParameter("name") != "" ? request
				.getParameter("name") : null;
		String email = request.getParameter("email") != null
				&& request.getParameter("email") != "" ? request
				.getParameter("email") : null;
		String pNum = request.getParameter("pNum") != null
				&& request.getParameter("pNum") != "" ? request
				.getParameter("pNum") : null;
		String univ = request.getParameter("univ") != null
				&& request.getParameter("univ") != "" ? request
				.getParameter("univ") : null;
		String grade = request.getParameter("grade") != null
				&& request.getParameter("grade") != "" ? request
				.getParameter("grade") : null;
		int langid1 = request.getParameter("langid1") != null
				&& request.getParameter("langid1") != "" ? Integer
				.parseInt(request.getParameter("langid1")) : 0;
		int langid2 = request.getParameter("langid2") != null
				&& request.getParameter("langid2") != "" ? Integer
				.parseInt(request.getParameter("langid2")) : 0;
		int langid3 = request.getParameter("langid3") != null
				&& request.getParameter("langid3") != "" ? Integer
				.parseInt(request.getParameter("langid3")) : 0;
		int langGrade1 = request.getParameter("langGrade1") != null
				&& request.getParameter("langGrade1") != "" ? Integer
				.parseInt(request.getParameter("langGrade1")) : 0;
		int langGrade2 = request.getParameter("langGrade2") != null
				&& request.getParameter("langGrade2") != "" ? Integer
				.parseInt(request.getParameter("langGrade2")) : 0;
		int langGrade3 = request.getParameter("langGrade3") != null
				&& request.getParameter("langGrade3") != "" ? Integer
				.parseInt(request.getParameter("langGrade3")) : 0;

		if (passwd == null || name == null || email == null || pNum == null) {

			Util.ShowError(response, "회원정보 수정에 실패했습니다.");

			return null;

		}
		univ=Util.toUTF8(univ);
		name=Util.toUTF8(name);
	
		
		user.setPasswd(passwd);
		user.setName(name);
		user.setEmail(email);
		user.setpNum(pNum);
		user.setUniv(univ);
		user.setGrade(grade);
		user.setLangid1(langid1);
		user.setLangGrade1(langGrade1);
		user.setLangid2(langid2);
		user.setLangGrade2(langGrade2);
		user.setLangid3(langid3);
		user.setLangGrade3(langGrade3);
		user.setUserid(userId);
		
		userDao.updateMemberInfo(user);
		
		Util.ShowSuccess(response, "개인정보 수정이 완료되었습니다.", "./questudy.jsp");
		
				
		return null;
	}

}
